package com.ruoyi.web.controller.vqms;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.vqms.domain.VQMSDriver;
import com.ruoyi.vqms.domain.VQMSQueueArea;
import com.ruoyi.vqms.domain.VQMSStorehouseLocation;
import com.ruoyi.vqms.domain.VQMSVisitRecordYuchai;
import com.ruoyi.vqms.service.IVQMSDriverService;
import com.ruoyi.vqms.service.IVQMSVisitRecordYuchaiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 访客（司机） 控制器
 */
@RestController
@RequestMapping("/vqms/driver")
public class VQMSDriverController extends BaseController {
    @Autowired
    private IVQMSDriverService driverService;

    /**
     * 查询访客（司机）列表(不分页)
     */
    @PreAuthorize("@ss.hasPermi('vqms:driver:list')")
    @RequestMapping("/getList")
    public AjaxResult getList(VQMSDriver vqmsDriver){
        return AjaxResult.success(driverService.selectList(vqmsDriver));
    }

    /**
     * 查询访客（司机）列表
     */
    @PreAuthorize("@ss.hasPermi('vqms:driver:list')")
    @RequestMapping("/list")
    public TableDataInfo list(VQMSDriver vqmsDriver){
        startPage();
        List<VQMSDriver> list=driverService.selectList(vqmsDriver);
        return getDataTable(list);
    }

    /**
     * 新增访客（司机）
     */
    @Log(title = "新增访客（司机）", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('vqms:driver:add')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody VQMSDriver vqmsDriver){
        VQMSDriver old=driverService.getByPhone(vqmsDriver.getPhone());
        if(old !=null){
            return AjaxResult.error("数据库已存在相同手机号，请更改手机号！");
        }
        vqmsDriver.setCreateBy(SecurityUtils.getUsername());
        driverService.add(vqmsDriver);
        return AjaxResult.success();
    }

    /**
     * 查询访客（司机）
     */
    @PreAuthorize("@ss.hasPermi('vqms:driver:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return AjaxResult.success(driverService.getById(id));
    }

    /**
     * 修改访客（司机）
     */
    @Log(title = "修改访客（司机）", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('vqms:driver:update')")
    @PutMapping(value = "/update")
    public AjaxResult update(@RequestBody VQMSDriver vqmsDriver)
    {
        vqmsDriver.setUpdateBy(SecurityUtils.getUsername());
        driverService.update(vqmsDriver);
        return AjaxResult.success();
    }

    /**
     * 删除访客（司机）
     */
    @Log(title = "删除访客（司机）", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('vqms:driver:delete')")
    @DeleteMapping(value = "/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        return AjaxResult.success(driverService.delete(ids));
    }

    /**
     * 导出访客（司机）
     */
    @Log(title = "导出访客（司机）", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('vqms:driver:export')")
    @PostMapping("/export")
    public AjaxResult export(VQMSDriver vqmsDriver, HttpServletResponse response) {
        List<VQMSDriver> list=driverService.selectList(vqmsDriver);
        ExcelUtil<VQMSDriver> util = new ExcelUtil<>(VQMSDriver.class);
        String filename = util.exportExcel(list, "访客(司机)").get("msg").toString();
        String downloadPath = RuoYiConfig.getDownloadPath() + filename;
        try {
            if (!FileUtils.checkAllowDownload(downloadPath)) {
                return AjaxResult.error("资源文件("+filename+")非法，不允许下载。 ");
            }
            FileUtils.writeBytes(downloadPath, response.getOutputStream());
            return AjaxResult.success(filename);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
}
